<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/23
 * Time: 下午5:06
 */
class Cart_api extends CI_Controller
{

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('cart_model');
        $this->load->model('user_model');
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
    }

    // 商城加入商品到购物车
    public function add_cart_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'goods_list', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $res = $this->db->query('select id as customer_id from app_customer where shop_user_id = ' . $post['shop_user_id'])->row_array();
            if (!$res['customer_id']) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "id不存在"));
            }
            $customer_id = $res['customer_id'];
            $flag = 1;
            $goods_list = json_decode($post['goods_list'], true);
            foreach ($goods_list as $good) {
                $product_id = $good['product_id'];
                $num = $good['num'];
                $res = $this->cart_model->add_cart($customer_id, $product_id, $num);
                if (!$res) {
                    $flag = 0;
                }
            }

            if ($flag) {
                $this->db->trans_commit();
                send_json(array('code' => 0, 'data' => base_url('cart/index'), 'msg' => "添加成功"));
            } else {
                $this->db->trans_rollback();
                send_json(array('code' => -1, 'data' => '', 'msg' => "添加失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }

    // 商城删除购物车的商品
    public function delete_cart_good_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'product_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $customer_id = $this->user_model->shop_user_id_to_customer_id($post['shop_user_id']);
            $product_id = $post['product_id'];
            $res = $this->cart_model->delete_good_from_cart($product_id, $customer_id);
            if ($res) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "删除成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "删除失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }

    }

    // 商城修改购物车商品选中状态
    public function change_cart_good_check_status_api()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_user_id', 'product_id', 'status', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            if ($post['status'] == 1) {
                $status = 1;
            } else {
                $status = 0;
            }
            $customer_id = $this->user_model->shop_user_id_to_customer_id($post['shop_user_id']);
            $res = $this->db->where(array("customer_id" => $customer_id, "product_id" => $post['product_id']))->update('app_cart', ['checked' => $status]);
            if ($res) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "修改成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "修改失败"));
            }
        }
    }

}